﻿Imports Microsoft.ApplicationBlocks.Data
Imports System.Data.SqlClient

Public Class frmBaoCaoTinhTrangHopDong

    Dim ofunction As New hDCommonFunction
    Dim ds As New DataSet

    Private Sub frmBaoCaoTinhTrangHopDong_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        cboThoiGian.Text = "Năm nay"
        LoadAllCombo()
        cboHopDong.Value = 2
    End Sub

    Private Sub btnThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThoat.Click
        Me.Close()
    End Sub

    Private Sub cboThoiGian_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboThoiGian.ValueChanged
        ofunction.XuLyThoiGian(dtTuNgay, dtDenNgay, cboThoiGian)
        cboHopDong_ValueChanged(sender, e)
    End Sub

    Private Sub LoadHopDong(ByVal iSoTinhTrangHD As Integer)
        Dim oArray As SqlParameter() = {New SqlParameter("@FROMDATE", SqlDbType.DateTime), _
                                        New SqlParameter("@TODATE", SqlDbType.DateTime), _
                                        New SqlParameter("@SoTinhTrangHD", SqlDbType.Int)}
        oArray(0).Value = dtTuNgay.Value
        oArray(1).Value = dtDenNgay.Value
        oArray(2).Value = iSoTinhTrangHD
        If (Not ds.Tables("HopDong") Is Nothing) Then
            ds.Tables("HopDong").Rows.Clear()
        End If
        SqlHelper.FillDataset(sConn, CommandType.StoredProcedure, "Proc_Report_HopDong_SoTinhTrangHD", ds, New String() {"HopDong"}, oArray)
        grdHopDog.DataSource = ds.Tables("HopDong")
    End Sub

    Private Sub LoadGridView()
        Try
            grdHopDog.DisplayLayout.Override.CellClickAction = Infragistics.Win.UltraWinGrid.CellClickAction.RowSelect
            With grdHopDog.DisplayLayout.Bands(0)
                .Columns("SoHopDong").Hidden = True
                .Columns("ChucVu").Hidden = True
                .Columns("GiaTriHD").Hidden = True
                .Columns("SoLoaiTien").Hidden = True
                .Columns("SoDoiTuong").Hidden = True
                .Columns("SoTinhTrangHD").Hidden = True
                .Columns("LyDoKetThuc").Hidden = True
                .Columns("LoaiHopDong").Hidden = True
                .Columns("NguoiKy").Hidden = True


                .Columns("MaHopDong").Header.Caption = "Số hợp đồng"
                .Columns("NgayKy").Header.Caption = "Ngày ký hợp đồng"
                .Columns("NgayHieuLuc").Header.Caption = "Ngày hiệu lực"
                .Columns("NgayKetThuc").Header.Caption = "Ngày kết thúc"


                .Columns("MaHopDong").Header.VisiblePosition = 1
                .Columns("NgayKy").Header.VisiblePosition = 2
                .Columns("NgayHieuLuc").Header.VisiblePosition = 3
                .Columns("NgayKetThuc").Header.VisiblePosition = 4
            End With
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try

    End Sub

    Private Sub btnXem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXem.Click
        Try
            Dim myReport As New rptTinhTranghopDong
            myReport.SetDataSource(ds.Tables("HopDong"))
            Dim fReport As New frmReportMain
            fReport.CrystalReportViewer1.ReportSource = myReport
            fReport.ShowDialog()
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try
    End Sub

    Private Sub LoadAllCombo()
        If (Not ds.Tables("TinhTrangHopDong") Is Nothing) Then
            ds.Tables("TinhTrangHopDong").Rows.Clear()
        End If
        SqlHelper.FillDataset(sConn, CommandType.StoredProcedure, "Get_All_TinhTrangHopDongs", ds, New String() {"TinhTrangHopDong"})
        If (Not ds.Tables("TinhTrangHopDong") Is Nothing) Then
            With cboHopDong
                .DataSource = ds.Tables("TinhTrangHopDong")
                .DisplayMember = "TenTinhTrangHopDong"
                .ValueMember = "SoTinhTrangHopDong"
                .DisplayLayout.Load(PathLayout & "cboTinhTrangHopDong.lyt")
            End With
        End If
    End Sub

    Private Sub cboHopDong_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboHopDong.ValueChanged
        If (cboHopDong.IsItemInList) Then
            LoadHopDong(cboHopDong.Value)
            LoadGridView()
        End If
    End Sub

End Class